package com.shinbada.modules.competitor.service;

		import com.google.common.collect.Maps;
		import com.shinbada.core.persistence.Page;
		import com.shinbada.core.service.CrudService;
		import com.shinbada.excel.ExcelKit;
		import com.shinbada.excel.handler.DefaultExcelReadStrategyHandlerV3;
		import com.shinbada.excel.modle.CheckResult;
		import com.shinbada.excel.modle.ImportExcelResult;
		import com.shinbada.excel.modle.StrategyCheckService;
		import com.shinbada.excel.modle.UploadModle;
		import com.shinbada.modules.competitor.entity.CompetitorNoumenonDataItem;
		import com.shinbada.modules.competitor.entity.CompetitorProductDataItem;
		import com.shinbada.modules.competitor.entity.DTO.ParamModelSelectionDTO;
		import com.shinbada.modules.competitor.mapper.CompetitorNoumenonDataItemMapper;
		import lombok.SneakyThrows;
		import org.springframework.stereotype.Service;
		import org.springframework.transaction.annotation.Transactional;
		import org.springframework.web.multipart.MultipartFile;

		import java.util.*;
		import java.util.stream.Collectors;

/**
 * 本体-数据项Service
 *
 * @author initcap
 * @version 2021-08-11
 */
@Service
@Transactional(readOnly = true)
public class CompetitorNoumenonDataItemService extends CrudService<CompetitorNoumenonDataItemMapper, CompetitorNoumenonDataItem> implements StrategyCheckService<CompetitorNoumenonDataItem> {

	private Map<String, String> keys;

	@Override
	public CompetitorNoumenonDataItem get(String id) {
		return super.get(id);
	}

	@Override
	public List<CompetitorNoumenonDataItem> findList(CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		return super.findList(competitorNoumenonDataItem);
	}

	@Override
	public Page<CompetitorNoumenonDataItem> findPage(Page<CompetitorNoumenonDataItem> page, CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		return super.findPage(page, competitorNoumenonDataItem);
	}

	@Transactional(readOnly = false)
	@Override
	public void save(CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		super.save(competitorNoumenonDataItem);
	}

	@Transactional(readOnly = false)
	@Override
	public void delete(CompetitorNoumenonDataItem competitorNoumenonDataItem) {
		super.delete(competitorNoumenonDataItem);
	}

	@Transactional(readOnly = false)
	@SneakyThrows
	public ImportExcelResult<CompetitorNoumenonDataItem> uploadExcel(MultipartFile file, UploadModle modle) {
		// 1. 准备提前数据
		readExcelInit();
		// 2. 添加数据处理器
		DefaultExcelReadStrategyHandlerV3<CompetitorNoumenonDataItem> excelReadHandler = new DefaultExcelReadStrategyHandlerV3<>(modle, this);
		// 3. 执行导入
		ExcelKit.$Import(CompetitorNoumenonDataItem.class).readXlsx(file.getInputStream(), excelReadHandler);
		// 4. 获得导入结果集
		ImportExcelResult<CompetitorNoumenonDataItem> importExcelResult = excelReadHandler.results();
		// 5. 批量添加成功数据
		importExcelResult.getSuccessList().forEach(this::save);
		// 返回结果,成功的数据不返回了，影响响应速度
		importExcelResult.setSuccessList(null);
		return importExcelResult;

	}

	/**
	 * @param entity 实体类
	 * @return 这个key的获取决定了，在Excel导入的时候需要校验的唯一标准
	 */
	private String getReadExcelKey(CompetitorNoumenonDataItem entity) {
		return entity.getId() + entity.getRemarks();
	}

	private void readExcelInit() {
		// 获取到现在数据库中的所有数据
		List<CompetitorNoumenonDataItem> db = mapper.findAllList(new CompetitorNoumenonDataItem());
		this.keys = Maps.newHashMapWithExpectedSize(db.size());
		// 转换键配置,请根据必要条件实现，直接使用会有问题
		db.forEach(entity -> this.keys.put(getReadExcelKey(entity), entity.getId()));
	}

	@Override
	public CheckResult readAndCheck(CompetitorNoumenonDataItem entity) {
		// 验证重复数据,请根据必要条件实现，直接使用会有问题
		String key = getReadExcelKey(entity);
		if (this.keys.containsKey(key)) {
			entity.setId(this.keys.get(key));
			return CheckResult.cover(key);
		}
		// 添加数据
		this.keys.put(key, "");
		return CheckResult.pass(key);
	}

	public List<CompetitorNoumenonDataItem> getTypeAndNameByDataItemValue(CompetitorProductDataItem competitorProductDataItem) {
		return mapper.getTypeAndNameByDataItemValue(competitorProductDataItem).stream().collect(
				Collectors.collectingAndThen(Collectors.toCollection(() ->
						new TreeSet<>(Comparator.comparing(CompetitorNoumenonDataItem::getNoumenonValue))), ArrayList::new)
		);
	}

	public List<CompetitorNoumenonDataItem> selectDataItemProperty(String[] noumenonIds) {
		mapper.selectNoumenonDataItemListByIds(noumenonIds);
		return null;
	}

	/**
	 * 根据本体id和值  获取本体id数组
	 *
	 * @param paramModelSelectionDTOList
	 * @return
	 */
	public String[] selectNoumenonByItemIdAndValue(List<ParamModelSelectionDTO> paramModelSelectionDTOList) {

		return mapper.selectNoumenonByItemIdAndValue(paramModelSelectionDTOList);
	}

	/**
	 * 根据名称数组获取对应得数值
	 *
	 * @param names
	 * @param
	 * @return
	 */
	public Map<String, List<CompetitorNoumenonDataItem>> getTypeAndNameByDataItemValues(String[] names) {
		Map<String, List<CompetitorNoumenonDataItem>> map = new HashMap<>();
		if (names != null && names.length > 0) {
			for (int i = 0; i < names.length; i++) {
				CompetitorProductDataItem competitorProductDataItem = new CompetitorProductDataItem();
				competitorProductDataItem.setType(CompetitorNoumenonDataItem.ATSNOUMENON);
				competitorProductDataItem.setName(names[i]);
				map.put(names[i], mapper.getTypeAndNameByDataItemValue(competitorProductDataItem).stream().collect(
						Collectors.collectingAndThen(Collectors.toCollection(() ->
								new TreeSet<>(Comparator.comparing(CompetitorNoumenonDataItem::getNoumenonValue))), ArrayList::new
						)
				));
			}
		}
		return map;
	}
}
